package com.mofang.controller;

import com.mofang.model.UserModel;
import com.pactera.framework.controller.BaseController;
import com.pactera.framework.exception.IException;

public class UserController extends BaseController {

	/** 登录 */
	private static final int LOGIN_KEY = 2;
	/** 退出登录 */
	private static final int LOGOUT_KEY = 3;

	public void loginFromRemote(
			final UpdateViewAsyncCallback<Boolean> viewUpdateCallback,
			final String userName, final String password) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						// throw new ServerException();
						return userModel.loginFromRemote(params[0], params[1]);
					}
				}, userName, password);

	}

	public void logout(UpdateViewAsyncCallback<Boolean> viewUpdateCallback) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGOUT_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<Void, Boolean>() {

					@Override
					public Boolean doAsyncTask(Void... params)
							throws IException {
						// return userModel.logout();
						return true;
					}
				});
	}

	/**
	 * cancel logout
	 */
	public void cancelLogout() {
		cancel(LOGOUT_KEY);
	}

	public void sendVerificationCode(
			UpdateViewAsyncCallback<Boolean> viewUpdateCallback, String phoneNo) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						return userModel.sendVerificationCode(params[0]);
					}
				}, phoneNo);
	}

	public void findPassword(
			UpdateViewAsyncCallback<Boolean> viewUpdateCallback,
			String phoneNo, String newPassword, String verificationCode) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						return userModel.findPassword(params[0], params[1],
								params[2]);
					}
				}, phoneNo, newPassword, verificationCode);
	}

	public void userRegister(
			UpdateViewAsyncCallback<Boolean> viewUpdateCallback,
			String register_invitationCode, String register_realName,
			String register_phone, String register_captcha,
			String register_password) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						return userModel.userRegister(params[0], params[1],
								params[2], params[3], params[4]);
					}
				}, register_invitationCode, register_realName, register_phone,
				register_captcha, register_password);
	}

	public void changeUserName(
			UpdateViewAsyncCallback<Boolean> viewUpdateCallback, String phone,
			String newName) {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						return userModel.changeUserName(params[0],params[1]);
					}
				}, phone,newName);
	}

	public void changeUserPassword(
			UpdateViewAsyncCallback<Boolean> viewUpdateCallback,
			String phoneNo, String newPassword, String verificationCode)  {
		final UserModel userModel = new UserModel();
		doAsyncTask(LOGIN_KEY, viewUpdateCallback,
				new DoAsyncTaskCallback<String, Boolean>() {
					@Override
					public Boolean doAsyncTask(String... params)
							throws IException {
						return userModel.changeUserPassword(params[0],params[1],params[2]);
					}
				}, phoneNo,newPassword,verificationCode);
	}
}
